package com.mall.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mall.ibatis.bean.Frame;
import com.mall.ibatis.bean.FunctionMethod;
import com.mall.ibatis.bean.ICell;
import com.mall.ibatis.bean.JQGrid;
import com.mall.ibatis.bean.Operator;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.OperatorDAO;
import com.ibatis.dao.client.DaoManager;

public class OperatorService
{
	private static final OperatorService instance = new OperatorService();
	
	private DaoManager daoManager = DaoConfig.getDaomanager();
	
	private OperatorDAO operatorDAO = null;
	
	private OperatorService()
	{
		operatorDAO  = (OperatorDAO) daoManager.getDao(OperatorDAO.class);
	}
	
	public static OperatorService getInstance()
	{
		return instance;
	}
	
	
	public List<Operator> selectAllOperator() throws Exception
	{
		return operatorDAO.selectAllOperator();
	}
	
	public List<Operator> selectAllOperator(Operator operator) throws Exception
	{
		return operatorDAO.selectAllOperator(operator);
	}
	
	public Integer selectAllOperatorCount(Operator operator) throws Exception
	{
		return operatorDAO.selectAllOperatorCount(operator);
	}
	
	
	public Operator selectOperator(Operator operator)  throws Exception
	{
		return operatorDAO.selectOperator(operator);
	}
	
	
	public String insertOperator(Operator operator) throws Exception
	{
		return operatorDAO.insertOperator( operator);
	}
	
	public void updateOperator(Operator operator) throws Exception
	{
		operatorDAO.updateOperator( operator);
	}
	
	public void updateOperatorWithoutPassword(Operator operator) throws Exception
	{
		operatorDAO.updateOperatorWithoutPassword( operator);
	}
	
	public void deleteOperator(Operator operator) throws Exception
	{
		operatorDAO.deleteOperator(operator);
	}
	
	public ReturnMessage validOperator(String username,String loginIP,String password) throws Exception
	{
		return operatorDAO.validOperator(username, loginIP, password);
	}
	
	public ReturnMessage modifyPassword(String operatorID, String oldPassword,
			String newPassword, String confirmPassword) throws Exception
	{
		return operatorDAO.modifyPassword(operatorID, oldPassword, newPassword, confirmPassword);
	}
	
	public void updateOperatorPassword(Operator operator)	throws Exception
	{
		operatorDAO.updateOperatorPassword(operator);
	}
	
	public JQGrid getOperatorList(List<Operator> operatorList,int page,int total) throws Exception
	{
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(page);
		jqGrid.setRecord(operatorList.size());
		jqGrid.setTotal(total);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for(Operator operator :operatorList)
		{
			rowsList.add(operator);
		}
		
		
		return jqGrid;
	}
	
	public JQGrid getOperatorList(List<Operator> operatorList,int page,int total,int records) throws Exception
	{
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(page);
		jqGrid.setRecord(records);
		jqGrid.setTotal(total);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for(Operator operator :operatorList)
		{
			rowsList.add(operator);
		}
		
		
		return jqGrid;
	}
	
	
	//通过用户的Id查询出相应的数据范围名称以及Id
	public String selectDataScope(String opId) {
		List<String> list = operatorDAO.selectDataScope(opId);
		if(list == null || list.size() == 0){
			return "<data>nodata</data>";
		}
		
		StringBuffer bf = new StringBuffer();
		bf.append("<data>");
		for(String s : list){
			bf.append(s + ",");
		}
		String s1 = bf.substring(0,bf.length() - 1);
		s1 = s1 + "</data>";
		
		return s1;
	}
	
	//通过用户的Id查询出相应的数据范围名称以及Id
	public List<String> selectDataScopeList(String opId) {
		List<String> list = operatorDAO.selectDataScope(opId);
		return list;
	}
	
	//保存用户和数据范围的对应关系
	public ReturnMessage addDataScope(String opId, String str) {
		ReturnMessage message = null;		
		daoManager.startTransaction();
		try{
			if(str.trim().length() == 0){
				operatorDAO.deleteDataScopeByOpId(opId);
				daoManager.commitTransaction();
				return message = new ReturnMessage("1");
			}
			operatorDAO.deleteDataScopeByOpId(opId);
			String[] s = str.split(",");
			for(String s1 : s){
				operatorDAO.addDataScope(opId,s1);
			}
			message = new ReturnMessage("1");
			daoManager.commitTransaction();
		}catch(Exception e){
			message = new ReturnMessage("0","保存失败" + e.getMessage());
		}finally{
			daoManager.endTransaction();
		}
		return message;
	}
	
	//向服务器请求勾选了哪些功能方法并返回给修改树状窗口
	public String methodIsCheck(String id) {
		//勾选的方法请求
		List<FunctionMethod> fms = operatorDAO.methodIsCheck(id);
		//勾选的功能请求
		List<String> functions = operatorDAO.functionIsCheck(id);
		StringBuffer bf = new StringBuffer();
		if(fms.size() > 0 || functions.size() > 0){
			bf.append("<str>");
			bf.append("<functionId>");
			for(String s : functions){
				bf.append(s + ",");
			}
			bf.append("</functionId>");
			bf.append("<methodId>");
			for(FunctionMethod fm : fms){
				bf.append(fm.getFunctionId() + fm.getMethodId() + ",");//组合id形成唯一
			}
			bf.append("</methodId>");
			bf.append("</str>");
			return bf.toString();
		}else{
			return bf.append("<str>notData</str>").toString();
		}
		
	}
	
	//方法与功能的对应关系的保存
	public ReturnMessage addMethod(String str,String id,String functionStr) {
		Set<String> set = new HashSet<String>();
		if(functionStr.trim().length() > 0){
			//把功能id的字符串组合分解成数组
			String[] functionStrs = functionStr.split(",");
			//用set集合来去掉重复
			for(String s : functionStrs){
				set.add(s);
			}
		}
		
		
		List<String[]> list = new ArrayList<String[]>();
		List<FunctionMethod> list2 = new ArrayList<FunctionMethod>();
		if(str.trim().length() > 0){
			//解析str字符串，并放入数组
			str = str.substring(1);
			String[] s = str.split(";");
			for(int i=0;i<s.length;i++){
				String[] s1 = s[i].split(",");
				list.add(s1);
			}
			
			//str字符串解析后放入对象集合
			for(String[] str1 : list){
				if(str1.length > 1){
					for(int i=1;i<str1.length;i++){
						FunctionMethod fm = new FunctionMethod();
						//每次为对象添加相应的functionId，每个功能对应增、删、改、
						fm.setRoleId(id);
						fm.setFunctionId(str1[0]);
						fm.setMethodId(str1[i]);
						list2.add(fm);
					}
				}
			}
		}

		daoManager.startTransaction();
		ReturnMessage message = null;
		try{
			//增加数据前先删除相关数据,按照功能的角色id号来删除
			operatorDAO.deleteMethodFunction(id);
			//先删除已经有的功能
			operatorDAO.deleteOperatorFunction(id);
			//批量插入功能集合
			for(String functionId : set){
				operatorDAO.addOperatorFunction(id,functionId);
			}
			String str3 = null;
			for(FunctionMethod fm : list2){
				str3 = operatorDAO.addMethodFunction(fm);//循环插入批量数据
				if(str == null){
					daoManager.endTransaction();
					return message = new ReturnMessage("0","添加失败");
				}
			}
			message = new ReturnMessage("1");
			
			daoManager.commitTransaction();
		}catch(Exception e){
			message = new ReturnMessage("0","添加失败" + e.getMessage());
		}finally{
			daoManager.endTransaction();
		}
		
		return message;
	}
	
	//管理架构功能树一次性加载
	public List<Frame> frame() {
		return operatorDAO.frame();
	}
	//ryh
	
	
	/**
	 * 得到街道收费人员列表
	 * @param street 街道编号
	 * @return 收费人员列表
	 * */
	@SuppressWarnings("unchecked")
	public List<Operator> selectStreetCharger(String street) throws Exception
	{
		return operatorDAO.selectStreetCharger(street);
	}


}